<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\supply\Category;
use app\common\model\supply\Supply as modelSupply;
use think\Validate;

/**
 * 供需接口
 */
class Supply extends Api
{
    protected $noNeedLogin = ['supply_category', 'lists', 'info'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (供需分类)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function supply_category()
    {
        $list = Category::all();
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (供需列表)
     * @ApiMethod (GET)
     * @ApiParams (name="category_id", type="integer", required=true, sample="", description="0=全部")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $param = request()->param();
        extract($param);
        $model = new modelSupply();
        if ($category_id) {
            $model->where('category_id', $category_id);
        }
        $list = $model->where('status', '1')->order('id desc')->paginate();
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (供需详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $id = request()->param('id', 0);
        if (!$id) {
            $this->error('参数错误');
        }
        $data = modelSupply::get($id);
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (供需删除)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function del()
    {
        $id = request()->param('id', 0);
        if (!$id) {
            $this->error('参数错误');
        }
        $res = modelSupply::destroy($id);
        if ($res) {
            $this->success('成功');
        }
        $this->error('失败');
    }
    /**
     * @Author LK
     * @ApiTitle (发布需求)
     * @ApiMethod (POST)    
     * @ApiParams (name="category_id", type="integer", required=true, sample="", description="分类")
     * @ApiParams (name="title", type="string", required=true, sample="", description="标题")
     * @ApiParams (name="content", type="string", required=true, sample="", description="内容")
     * @ApiParams (name="name", type="string", required=true, sample="", description="联系人")
     * @ApiParams (name="mobile", type="string", required=true, sample="", description="手机号")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function addpub()
    {
        $param = request()->post();
        extract($param);
        $validate = new Validate([
            'category_id'   => 'require|gt:0',
            'title'  => 'require',
            'content'  => 'require',
            'name'  => 'require',
            'mobile'  => 'require',
        ], [
            'category_id.require'  => '请选择分类',
            'title.require' => '标题不能为空',
            'content.require' => '内容不能为空',
            'name.require' => '联系人不能为空',
            'mobile.require' => '联系方式不能为空',
        ]);
        $result = $validate->check($param);
        if (!$result) {
            $this->error(__($validate->getError()));
        }
        $param['sn'] = sn('supply', 'sn', 'SY');
        $param['user_id'] = $this->auth->id;
        $model = new modelSupply($param);
        $res = $model->allowField(true)->save();
        if ($res) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }
    /**
     * @Author LK
     * @ApiTitle (我的发布)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function my_supply()
    {
        $model = new modelSupply();
        $list = $model->where('user_id', $this->auth->id)->paginate();
        $this->success('', $list);
    }
}
